package br.com.amadeus.android.service;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;

import br.com.amadeus.android.activities.home.R;
import br.com.amadeus.android.business.FacadeAmadeusDroid;
import br.com.amadeus.android.entities.Config;
import br.com.amadeus.android.entities.Forum;
import br.com.amadeus.android.entities.Message;
import br.com.amadeus.android.util.ConstantsAmadeusDroid;
import br.com.amadeus.android.util.ImageUtil;
import br.com.amadeus.android.util.JSONReader;
import br.com.amadeus.android.util.RESTFileContent;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class ServiceSyncForumMessages extends Service implements Runnable{
	protected static final String CATEGORIA = "curso";

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		new Thread(this).start();
	}
	
	public void run() {
				
		Config cfg = FacadeAmadeusDroid.getInstance(this).getConfig();
		String url = cfg.getAmadeus_url() + ConstantsAmadeusDroid.HOST_AMADEUS_WEB_FORUM_MESSAGES;
		RESTFileContent rfc = new RESTFileContent();
		InputStream is;
		String resultJson;
		ArrayList<Forum> listForum;
		try {
			is = rfc.getRESTFileContent(url);
			resultJson = rfc.toString(is);
			listForum = JSONReader.getForumListMessage(resultJson);
			for (Forum forum : listForum) {
				ArrayList<Message> listMessage = forum.getListMessages();
				getPhotoAuthors(listMessage);
			}
			
			boolean test = FacadeAmadeusDroid.getInstance(this).syncronizeForumMessages(listForum);
			if (test) {
				createNotification();
			}
			
		} catch (ClientProtocolException e) {
			Log.e(CATEGORIA, e.getMessage(), e);
		} catch (IOException e) {
			Log.e(CATEGORIA, e.getMessage(), e);
		} catch (JSONException e) {
			Log.e(CATEGORIA, e.getMessage(), e);
		}
		stopSelf();
	}

	private void getPhotoAuthors(ArrayList<Message> listMessage) {
		
		Config cfg = FacadeAmadeusDroid.getInstance(this).getConfig();
		for (Message message : listMessage) {
			String endImageAuthor = cfg.getAmadeus_url() + 
			ConstantsAmadeusDroid.HOST_AMADEUS_WEB_PHOTO + message.getPersonLogin();
			try {
				ImageUtil.saveImageUser(endImageAuthor, message.getPersonLogin());
			} catch (ClientProtocolException e) {
				Log.e(CATEGORIA, e.getMessage(), e);
			} catch (IOException e) {
				Log.e(CATEGORIA, e.getMessage(), e);
			}
		}
		
	}

	private void createNotification() {
		
		String mensagemBarraStatus = ConstantsAmadeusDroid.TICKER_TEXT_NOTIFICATION;
		
		CharSequence titulo = ConstantsAmadeusDroid.TITLE_NOTIFICATION;
		
		String mensagem = ConstantsAmadeusDroid.MESSAGE_NOTIFICATION_FORUM;
		
		NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		
		Intent it = new Intent("HOME");
		
		PendingIntent p = PendingIntent.getActivity(this, 0, it, 0);
		
		Notification n = new Notification(R.drawable.amadeus_notification, mensagemBarraStatus, System.currentTimeMillis());
		
		n.setLatestEventInfo(this, titulo, mensagem, p);
		
		n.vibrate = new long[] { 100, 250, 100, 500 };

		nm.notify(R.string.app_name, n);
		
		
	}

}
